﻿<%@ Control Language="C#" Inherits="System.Web.Mvc.ViewUserControl<dynamic>" %>
<form action=''>
    <fieldset>
    <legend>Заказы по дате назначения:</legend>
    <p>            
        <% Html.RenderPartial("EditorTemplates/DateTimeTemplateViewData_Calend",
            new web_uchet.Views.Shared.TemplateClasses.
            DateTimeTemplateViewData("DateStart", "DateStart", "Начальная дата:", null)); %>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
        <% Html.RenderPartial("EditorTemplates/DateTimeTemplateViewData_Calend",
            new web_uchet.Views.Shared.TemplateClasses.
            DateTimeTemplateViewData("DateEnd", "DateEnd", "Конечная дата:", null)); %>
        &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
        <input type="button" value="Получить" class="button" id="btn-get-orders-by-range"/>
    </p>
    </fieldset>
</form>
<script type="text/javascript">
    
    String.prototype.setCharAt = function (index, chr) {
        if (index > this.length - 1) return str;
        return this.substr(0, index) + chr + this.substr(index + 1);
    }
    jQuery.noConflict();
    jQuery(document).ready(function () {

        //Получаем даты с проверкой на формат 1.1.2001 =>01.01.2001
        var date = new Date();
        var temp = (date.getDate() + 0).toString();
        if (temp.length == 1) temp = '0' + temp;
        var RusFormattedStDate = temp + '-';
        temp = (date.getMonth() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        RusFormattedStDate = RusFormattedStDate + temp + '-' + date.getFullYear().toString() + ' 00:00';

        temp = (date.getDate() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        var RusFormattedEndDate = temp + '-';
        temp = (date.getMonth() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        RusFormattedEndDate = RusFormattedEndDate + temp + '-' + date.getFullYear().toString() + ' 00:00';

        temp = (date.getMonth() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        var USFormattedStDate = temp + '-';
        temp = (date.getDate() + 0).toString();
        if (temp.length == 1) temp = '0' + temp;
        USFormattedStDate = USFormattedStDate + temp + '-' + date.getFullYear().toString() + ' 00:00';

        temp = (date.getMonth() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        var USFormattedEndDate = temp + '-';
        temp = (date.getDate() + 1).toString();
        if (temp.length == 1) temp = '0' + temp;
        USFormattedEndDate = USFormattedEndDate + temp + '-' + date.getFullYear().toString() + ' 00:00';
        jQuery("#DateStart").val(RusFormattedStDate );
        jQuery("#DateEnd").val(RusFormattedEndDate );

        jQuery('#btn-get-orders-by-range').click(function () {
            var startUS = jQuery("#DateStart").val().toString();
            var startRUS = jQuery("#DateStart").val().toString();
            startUS = startUS.setCharAt(3, startRUS.charAt(0));
            startUS = startUS.setCharAt(4, startRUS.charAt(1));
            startUS = startUS.setCharAt(0, startRUS.charAt(3));
            startUS = startUS.setCharAt(1, startRUS.charAt(4));

            var endUS = jQuery("#DateEnd").val().toString();
            var endRUS = jQuery("#DateEnd").val().toString();
            endUS = endUS.setCharAt(3, endRUS.charAt(0));
            endUS = endUS.setCharAt(4, endRUS.charAt(1));
            endUS = endUS.setCharAt(0, endRUS.charAt(3));
            endUS = endUS.setCharAt(1, endRUS.charAt(4));

            var grid = jQuery('#' + '<%= ViewData["GridId"].ToString() %>');
            grid.jqGrid('setGridParam', { url: '/ViewOrder/DateRange?DateStart=' + startUS + '&DateEnd=' + endUS,
                postData: { FilterType: 'DateRange', FilterParameter: startRUS + '_' + endRUS }
            }).trigger("reloadGrid");
        });
    });   
</script>